package day13;

import java.util.Collections;
import java.util.List;
import org.junit.Test;

import java.util.ArrayList;

//测试集合排序
public class TestSort {
    @Test
    public void sortNumber(){
        List<Integer> list=new ArrayList();
        list.add(10);
        list.add(130);
        list.add(60);
        list.add(30);
        list.add(30);
        list.add(20);
        System.out.println("排序前："+list);
        //默认数字升序
        //如果想变成降序
        //Comparator<Integer> com=(o1,o2)-> o2-o1;

        Collections.sort(list, ((o1,o2)-> o2-o1));
        System.out.println("排序后:"+list);
    }

    @Test
    public void test(){
        List<String> list=new ArrayList<>();
        list.add("aaa");
        list.add("abc");
        list.add("bbb");
        list.add("bac");
        list.add("ddd");
        //降序排序 同样添加比较器对象
        System.out.println("排序前："+list);
        Collections.sort(list,(o2,o1)-> o1.compareTo(o2));
        System.out.println("排序后："+list);
    }

    @Test
    public void sortObject(){
        List<Employee> list=new ArrayList<>();
        list.add(new Employee(100,"张三",8000));
        list.add(new Employee(200,"李四",12000));
        list.add(new Employee(300,"王五",12000));
        list.add(new Employee(500,"茜茜",34000));
        list.add(new Employee(400,"大马猴",6000));
        //排序规则：先根据工资降序排列，工资相同按照员工编号升序排列
        Collections.sort(list,(o1,o2)->{
            int n=(int)(o1.sal-o2.sal);
            if (n==0){
                n=o2.empno-o1.empno;
            }
            return n;
        });
        for (Employee e:list){
            System.out.println(e.empno+" "+e.name+" "+e.sal);
        }
    }
}

class Employee{
    int empno;
    String name;
    double sal;

    public Employee(int empno, String name, double sal) {
        this.empno = empno;
        this.name = name;
        this.sal = sal;
    }

}